package com.bm.specialvehiclefordispatcher.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;

import com.bm.specialvehiclefordispatcher.bean.City;
import com.bm.specialvehiclefordispatcher.bean.SelectCity;
import com.bm.specialvehiclefordispatcher.bean.TCity;
import com.google.gson.Gson;

public class CityCreator {
	
	private static final String FILE_ALL = "cities.json";
	private static final String FILE_SELECT = "select_cities.json";
	private static final String CHARSET = "GBK";
	/**
	 * 选择城市
	 */
	private static List<City> selectCities;
	/**
	 * 省市区
	 */
	private static List<TCity> tCities;
	
	public static void getCityMaps(final Activity context, final OnCityLoadedListener listener){
		if(tCities != null) {
			if(listener != null)
				listener.onCityLoaded(tCities);
			return;
		}
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				tCities = new ArrayList<TCity>();
				String json = getJsonString(context, FILE_ALL);
				JSONObject data;
				List<TCity> cities = null;
				try {
					data = new JSONObject(json);
					JSONArray array = data.getJSONArray("leverArea");
					cities = arrayToTCities(array);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				if(cities == null) {
					if(listener != null)
						listener.onCityLoaded(null);
					return;
				}
					
				
				for (int i = 0; i < cities.size(); i++) {
					TCity tCity = cities.get(i);
					if (tCity.nextArea.get(0).nextArea == null) {
						List<TCity> nextArea = tCity.nextArea;
						List<TCity> tmp = new ArrayList<TCity>();
						TCity tmpCity = null;
						try {
							tmpCity = (TCity) tCity.clone();
						} catch (CloneNotSupportedException e) {
							e.printStackTrace();
						}
						tmpCity.nextArea = nextArea;
						tmp.add(tmpCity);
						tCity.nextArea = tmp;
					}
					tCities.add(tCity);
				}
				
				if(listener != null)
					listener.onCityLoaded(tCities);
			}
		}).start();
			
	}
	
	private static List<TCity> arrayToTCities(JSONArray array) throws JSONException {
		List<TCity> cities = new ArrayList<TCity>();
		for (int i = 0; i < array.length(); i++) {
			JSONObject object = array.getJSONObject(i);
			TCity city = new TCity();
			city.id = object.getInt("id");
			city.first = object.getString("first");
			city.full = object.getString("full");
			city.fullName = object.getString("fullName");
			city.name = object.getString("name");
			JSONArray tmp = null;
			try {
				tmp = object.getJSONArray("nextArea");
			} catch (Exception e) {
				cities.add(city);
				continue;
			}
			city.nextArea = arrayToTCities(tmp);
			cities.add(city);
		}
		
		return cities;
	}
	
	public static String getJsonString(Context context, String fileName){
		String json = "";
		try {
			InputStream in = context.getAssets().open(fileName);
			int lenght = in.available();
			byte[] buffer = new byte[lenght];
			in.read(buffer);
			json = EncodingUtils.getString(buffer, CHARSET);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	public static List<City> getSelectCities(Context context){
		if(selectCities != null)
			return selectCities;
		
		selectCities = new ArrayList<City>();
		String json = getJsonString(context, FILE_SELECT);
		
		Gson gson = new Gson();
		SelectCity tmp = gson.fromJson(json, SelectCity.class);
		selectCities = tmp.allCities;
		
		return selectCities;
	}
	
	public static interface OnCityLoadedListener {
		void onCityLoaded(final List<TCity> cities);
	}

}
